define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'carbon/coefficient/info/index' + location.search,
                    add_url: 'carbon/coefficient/info/add',
                    edit_url: 'carbon/coefficient/info/edit',
                    del_url: 'carbon/coefficient/info/del',
                    multi_url: 'carbon/coefficient/info/multi',
                    import_url: 'carbon/coefficient/info/import',
                    table: 'carbon_coefficient_info',
                }
            });
            this.table.first();
            var maxLevel = Config['maxLevel'];
            if (maxLevel == 2) {
                this.table.second();
            }
        },
        table: {
            first: function () {
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'carbon/coefficient/Info/table1',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id') },
                            { field: 'type_id', title: __('Type_id'), visible: false },
                            { field: 'type.name', title: __('Type') },
                            { field: 'name', title: __('Name'), operate: 'LIKE', table: table1, class: 'autocontent', formatter: Table.api.formatter.content },
                            { field: 'value', title: __('Value'), operate: 'BETWEEN' },
                            { field: 'unit', title: __('Unit'), operate: 'LIKE', table: table1, class: 'autocontent', formatter: Table.api.formatter.content },
                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                                buttons: [{
                                    name: 'log',
                                    title: '省份系数',
                                    text: '省份系数',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-primary btn-xs btn-click',
                                    click: function (e, data) {
                                        $("#myTabContent2 .form-commonsearch input[name='superior_id']").val(data.id);
                                        $("#myTabContent2 .btn-refresh").trigger("click");
                                    },
                                    visible: function (e) {
                                        if (e.exist == true)
                                            return !0
                                    },
                                }],
                                align: 'right'
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
                // 绑定过滤事件
                $('.qweasd1 a[data-toggle="tab"]', table1.closest(".panel-intro")).on('click', function (e) {
                    var value = $(this).data("value");
                    var options = table1.bootstrapTable('getOptions');
                    var queryParams = options.queryParams;
                    options.pageNumber = 1;
                    options.queryParams = function (params) {
                        //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                        params = queryParams(params);
                        //如果希望追加搜索条件,可使用
                        var filter = params.filter ? JSON.parse(params.filter) : {};
                        var op = params.op ? JSON.parse(params.op) : {};
                        if (value) {
                            //这里可以自定义多个筛选条件
                            filter.type_id = value;
                            op.type_id = '=';
                        } else {
                            //选全部时要移除相应的条件
                            delete filter.type_id;
                            delete op.type_id;
                        }

                        params.filter = JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                        return params;
                    };
                });
            },

            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'carbon/coefficient/Info/table2',
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id') },
                            { field: 'superior_id', title: __('上级ID'), visible: false },
                            { field: 'name', title: __('Name'), operate: 'LIKE', table: table2, class: 'autocontent', formatter: Table.api.formatter.content },
                            { field: 'value', title: __('Value'), operate: 'BETWEEN' },
                            { field: 'unit', title: __('Unit'), operate: 'LIKE', table: table2, class: 'autocontent', formatter: Table.api.formatter.content },
                            { field: 'province.name', title: __('Province') },
                            { field: 'province_id', title: __('Province_id'), visible: false },
                            { field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
                $('.filter-type ul li a', table2.closest(".panel-intro")).on('click', function (e) {
                    $(this).closest("ul").find("li").removeClass("active");
                    $(this).closest("li").addClass("active");
                    var field = 'province_id';
                    var value = $(this).data("value") || '';
                    var object = $("[name='" + field + "']", table2.closest(".bootstrap-table").find(".commonsearch-table"));
                    if (object.prop('tagName') == "SELECT") {
                        $("option[value='" + value + "']", object).prop("selected", true);
                    } else {
                        object.val(value);
                    }
                    table2.trigger("uncheckbox");
                    table2.bootstrapTable('refresh', { pageNumber: 1 });
                });
            },
        },

        add: function () {
            Controller.api.bindevent();
            // $(document).ready(function () {
            //     // 监听省份选择的变化
            //     $('#c-province_id').on('change', function () {
            //         var provinceId = $(this).val(); // 获取选中的省份ID

            //         if (provinceId) {
            //             // 更新城市选择框的data-source为对应省份的城市
            //             $("#c-city_id").data("source", "area/city?province_id=" + provinceId);

            //             // 重新加载SelectPage的数据
            //             $("#c-city_id").selectPageClear(); // 清空当前选择
            //             $("#c-city_id").selectPageRefresh(); // 刷新SelectPage
            //         } else {
            //             // 如果没有选择省份，清空城市下拉框
            //             $("#c-city_id").selectPageClear();
            //         }
            //     });
            // });

        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
